package com.itboye.ihomebank.util;

import java.util.ArrayList;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.widget.choosecity.CityModel;
import com.itboye.ihomebank.db.CityNoDBManager;
import com.itboye.ihomebank.widget.wheelview.OnWheelChangedListener;
import com.itboye.ihomebank.widget.wheelview.WheelView;
import com.itboye.ihomebank.widget.wheelview.adapter.AbstractWheelTextAdapter02;

import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ChangeAddressDialog extends Dialog {
	
	private Context context;
	/**城市*/
	private WheelView wvProvince;
	/**区域*/
	private WheelView wvCitys;
	
	/**确认*/
	private TextView btnSure;
	/**取消*/
	private TextView btnCancel;
	
	private AddressTextAdapter provinceAdapter;
	
	
	private ArrayList<CityModel> arrayLitArea;// 区域列表
	
	private String strProvince = "杭州市";
	private String strCity = "滨江区";
	private SQLiteDatabase database;
	private CityNoDBManager cityNoDBManager;
	
	
	private int maxsize = 24;
	private int minsize = 14;
	public ChangeAddressDialog(Context context) {
		super(context, R.style.ShareDialog);
		this.context = context;
	}
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_myinfo_changeaddress);
		
		init();
		
	}
	
	private void init(){
		wvProvince=(WheelView) findViewById(R.id.wv_address_province);
		wvCitys=(WheelView) findViewById(R.id.wv_address_city);
		btnSure=(TextView) findViewById(R.id.btn_myinfo_sure);
		btnCancel=(TextView) findViewById(R.id.btn_myinfo_cancel);
		
		arrayLitArea = cityNoDBManager.queryArea(" ");
		
		wvCitys.addChangingListener(new OnWheelChangedListener() {
			
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				
			}
		});
	}
	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityModel> getCityNames() {
		ArrayList<CityModel> names = new ArrayList<CityModel>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM T_City ORDER BY NameSort", null);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityModel cityModel = new CityModel();
			cityModel.setCityName(cursor.getString(cursor
					.getColumnIndex("CityName")));
			cityModel.setNameSort(cursor.getString(cursor
					.getColumnIndex("NameSort")));
			names.add(cityModel);
		}
		return names;
	}
	
	/**
	 * 列表显示适配器
	 *
	 */
	private class AddressTextAdapter extends AbstractWheelTextAdapter02 {
		ArrayList<String> list;

		protected AddressTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
			super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
			this.list = list;
			setItemTextResource(R.id.tempValue);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return list.get(index) + "";
		}
	}
	
	
}
